/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.util.Properties;
import java.util.regex.Pattern;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author sevelindo parado
 */
public class EMailControl {
    
    public static boolean sendMail(String subject, String body, String from) {
        final String username = "satacredito@gmail.com";
        final String password = "arquitectura2012";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("satacredito@gmail.com"));
            message.setSubject(subject);
            message.setContent("<p><FONT SIZE = +2 COLOR = \"BLUE\">Enviado por: </FONT> <FONT SIZE = +1 COLOR = \"BLUE\">" + from + "</FONT><BR><BR></p>"
                    + "<p> <FONT SIZE = +2 COLOR = \"RED\">Descripcion del problema: </FONT><BR><BR><FONT SIZE = +1><PRE>" + body + "</PRE></FONT></p>", "text/html");
//            message.addHeader("MyHeaderName", "myHeaderValue");
            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("no se envio el mensaje");
            return false;
        }
    }

        
    /**
     * Emails that match:
     *   1. “mkyong@yahoo.com”, “mkyong-100@yahoo.com”,”mkyong.100@yahoo.com”
     *   2. “mkyong111@mkyong.com”, “mkyong-100@mkyong.net”,”mkyong.100@mkyong.com.au”
     *   3. “mkyong@1.com”, “mkyong@gmail.com.com”
     * Emails that doesn’t match:
     *   1. “mkyong” – must contains “@” symbol
     *   2. “mkyong@.com.my” – tld can not start with dot “.”
     *   3. “mkyong123@gmail.a” – “.a” is not a valid tld, last tld must contains at least two characters
     *   4. “mkyong123@.com” – tld can not start with dot “.”
     *   5. “mkyong123@.com.com” – tld can not start with dot “.”
     *   6. “.mkyong@mkyong.com” – email’s first character can not start with dot “.”
     *   7. “mkyong()*@gmail.com” – email’s is only allow character, digit, underscore and dash
     *   8. “mkyong@%*.com” – email’s tld is only allow character and digit
     *   9. “mkyong..2002@gmail.com” – double dots “.” are not allow
     *   10. “mkyong.@gmail.com” – email’s last character can not end with dot “.”
     *   11. “mkyong@mkyong@gmail.com” – double “@” is not allow
     *   12. “mkyong@gmail.com.1a” -email’s tld which has two characters can not contains digit
     * @param emailAddress
     * @return 
     */
    public static boolean validEmail(final String emailAddress){
        final String emailPattern="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        return Pattern.compile(emailPattern).matcher(emailAddress).matches();
    }
    
}
